<?php
namespace Admin\Controller;

class AdminController extends BaseController {


  
    public function index() {
       
        $this->display();
    }

   /**
    **获取管理员信息
   */
   public function get_admin() {
        $this->verify_request();

        $page = $_POST['page'];
        $rows = $_POST['rows'];
        $limit = "limit ".($page-1)*$rows.','.$rows;

        $Admin = D ('Admin');

        $this->ajaxReturn($Admin->get_admin_by_limit($limit));

   }

  /**
   **添加管理员
   */
   public function add() {
      //die('11');
        $Admin = D ('Admin');

        //验证用户名
        if(!preg_match("/^[a-zA-Z][a-zA-Z0-9|_]{2,19}$/",$_POST['username'])) 
          $this->ajaxReturn(array('errorMsg' => "用户名格式错误"));
       
        //验证联系方式
        if(!preg_match("/^13[0-9]{1}[0-9]{8}$|15[012356789]{1}[0-9]{8}$|18[012356789]{1}[0-9]{8}$|14[57]{1}[0-9]$/",$_POST['telphone'])) 
          $this->ajaxReturn(array('errorMsg' => "联系方式格式错误"));

        //验证邮箱
        if(!preg_match("/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i",$_POST['email']))
          $this->ajaxReturn(array('errorMsg' => "邮箱格式错误"));

        //判断站点管理员是否存在   
        if($rs = $Admin->where(" username = '$_POST[username]'")->find()){
            $this->ajaxReturn(array('errorMsg' => "管理员已存在"));
        }

        if ($_POST['pwd'] !== $_POST['truepassword']){
          $this->ajaxReturn(array('errorMsg' => "两次密码不一致"));  
        }

        
        //获取值
        $data['username'] = $_POST['username'];
        $data['tname'] = $_POST['tname'];
        $data['email'] = $_POST['email'];
        $data['password'] = md5($_POST['pwd']);
        $data['telphone'] =$_POST['telphone'];
        $data['super'] = 0;
        $data['status'] =$_POST['status'];
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['last_login_time'] = date('Y-m-d H:i:s');
        $data['loginip'] =get_client_ip();

        if(empty($_POST['perm']))
          $this->ajaxReturn(array('errorMsg' => "请设置管理员权限！")); 
        $data['perm'] = implode(',',$_POST['perm']);


        // 添加
        if ($flag = $Admin->add($data) !== false)
          $this->ajaxReturn(array('success' => true));
        else 
          $this->ajaxReturn(array('errorMsg'=>'添加失败'));
          
   }

   /**
   **修改管理员
   */
   public function edit() {
        $Admin = M ('Admin');

        $id = !empty($_GET['id'])?$_GET['id']:NULL;
       // echo $Admin->getlastsql();die;
        if (empty($id)) $this->ajaxReturn(array('errorMsg'=>'未知管理员'));
        if (!$rs = $Admin->where(" id = $id ")->find()) 
            $this->ajaxReturn(array('errorMsg'=>'管理员不存在'));
       

       //验证用户名
        if(!preg_match("/^[a-zA-Z][a-zA-Z0-9|_]{2,19}$/",$_POST['username'])) 
              $this->ajaxReturn(array('errorMsg' => "用户名格式错误"));

        //验证联系方式
        if(!preg_match("/^13[0-9]{1}[0-9]{8}$|15[012356789]{1}[0-9]{8}$|18[012356789]{1}[0-9]{8}$|14[57]{1}[0-9]$/",$_POST['telphone'])) 
              $this->ajaxReturn(array('errorMsg' => "联系方式格式错误"));

        //验证邮箱
        if(!preg_match("/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i",$_POST['email']))
          $this->ajaxReturn(array('errorMsg' => "邮箱格式错误"));
       
        if ($_POST['pwd'] !== $_POST['truepassword']){
          $this->ajaxReturn(array('errorMsg' => "两次密码不一致"));  
        }
        //判断用户有没有修改密码
        if ($_POST['pwd']=="default") {
          $u = $Admin->where(" id = $id ")->find();
          $data['password'] = $u['password'];
        } else {
          $data['password'] = md5($_POST['pwd']);
        }

        //获取值
        $data['tname'] = $_POST['tname'];
        $data['email'] = $_POST['email'];
        $data['telphone'] =$_POST['telphone'];
        $data['super'] =0;
        $data['status'] =$_POST['status'];
        $data['last_login_time'] = date('Y-m-d H:i:s');
        $data['loginip'] =get_client_ip();
        
        if(empty($_POST['perm']))
          $this->ajaxReturn(array('errorMsg' => "请设置管理员权限！")); 
        $data['perm'] = implode(',',$_POST['perm']);

        if ($flag = $Admin->where('id = '.$id)->save($data) !== false) 
            $this->ajaxReturn(array('success'=>true));

        $this->ajaxReturn(array('errorMsg'=>'修改管理员信息失败'));

   }


   /**
   **删除管理员
   */
   public function del() {

        $Admin = D ('Admin');

        $id = $_POST['id'];
        if (empty($id)) $this->ajaxReturn(array('errorMsg'=>'未知管理员'));
        if (!$rs = $Admin->where("id = $id ")->find())
         $this->ajaxReturn(array('errorMsg'=>'管理员不存在'));

        if (!$Admin->where("id = $id ")->delete()) 
          $this->ajaxReturn(array('errorMsg'=>'删除失败'));
        $this->ajaxReturn(array('success'=>true));
    }


    /**
     * 修改密码
     * @return json
     */
    public function edit_pwd(){
         $this->verify_request();
         $pwd = $_POST['pwd'];
         $rpwd = $_POST['rpwd'];
         $oldpwd = $_POST['oldpwd'];

         $Admin = D("Admin");
         $admin = session('admin');
         $id = $admin['id'];
         if($oldpwd != 'default'){
            if(md5($oldpwd) != $admin['password'])
               $this->ajaxReturn(array("errorMsg"=>"旧密码输入错误！"));
         }
         if($pwd != $rpwd)
            $this->ajaxReturn(array("errorMsg"=>"两次密码不一致！"));
         $data['password'] = md5($pwd);
         if($flag = $Admin->where(" id = $id ")->save($data) !== false){
             session('admin',null);
             $this->ajaxReturn(array("success"=>true));
         }else{
            $this->ajaxReturn(array("errorMsg"=>"修改密码失败！"));
         }

    }

    /**
     * 编辑个人信息
     * @return json
     */
    public function edit_info(){
         $this->verify_request();
         $Admin = D("Admin");
         $admin = session('admin');

         //验证联系方式
         if(!preg_match("/^13[0-9]{1}[0-9]{8}$|15[012356789]{1}[0-9]{8}$|18[012356789]{1}[0-9]{8}$|14[57]{1}[0-9]$/",$_POST['telphone'])) 
          $this->ajaxReturn(array('errorMsg' => "联系方式格式错误"));

              //验证邮箱
        if(!preg_match("/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i",$_POST['email']))
          $this->ajaxReturn(array('errorMsg' => "邮箱格式错误"));

         $data['tname'] = $_POST['tname'];
         $data['email'] = $_POST['email'];
         $data['telphone'] = $_POST['telphone'];
         p($_POST['tname']);

         if($flag = $Admin->where("id = ".$admin['id'])->save($data) !==false){
            $this->ajaxReturn(array("success"=>true));
         }else{
            $this->ajaxReturn(array("errorMsg"=>"修改资料失败！"));
         }
    }
   
}